# Copyright (c) 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# GNU Makefile based on shared rules provided by the Native Client SDK.
# See README.Makefiles for more details.

CFLAGS :=-g -O0 -pthread -D__ANDROID__ -DANDROID -DBIONIC -fPIC -Wno-incompatible-library-redeclaration

LIBC_CFLAGS += -I$(SRC_ROOT)/stdlib -I$(SRC_ROOT)string -I$(SRC_ROOT)/stdio
LIBC_CFLAGS += -I$(SRC_ROOT)/private -I$(SRC_ROOT) -I$(SRC_ROOT)/arch-nacl/syscalls
LIBC_CFLAGS += -I$(SRC_ROOT)/upstream-netbsd

LIBC_CFLAGS +=-MD -DNACL_SDK_DEBUG -include AndroidConfig.h -Wstrict-aliasing=2 
LIBC_CFLAGS +=-U_FORTIFY_SOURCE -DANDROID_CHANGES -fno-short-enums -funwind-tables -fno-stack-protector -fno-strict-aliasing
LIBC_CFLAGS +=-fno-exceptions -fno-omit-frame-pointer -fno-asynchronous-unwind-tables -fno-unwind-tables
LIBC_CFLAGS +=-DINET6 -DUSG_COMPAT -DANDROID_SMP=1 -D_GNU_SOURCE -DHAVE_MORECORE=0 -D_USE_NACL_SYS_ALIAS_=1 -fPIC

ARM_FLAGS :=-mcpu=cortex-a9 -mfloat-abi=hard -mfpu=neon
ARM_FLAGS +=-DTM_GMTOFF=tm_gmtoff -DUSG_COMPAT=1 -DHAVE_ARM_TLS_REGISTER
ARM_FLAGS +=-gdwarf-3

X86_FLAGS :=-DUSES_SSE2 -DFLOATING_POINT -D_BYTE_ORDER=_LITTLE_ENDIAN

ifeq ($(NACL_ARCH),arm-nacl)
ARCH_FLAGS:=$(ARM_FLAGS)
else
ARCH_FLAGS:=$(X86_FLAGS)
endif


all: irt compile

.PHONY : irt
IRT_PY := $(SRC_ROOT)/../../tools/irt_syscalls.py

IRT_SYSCALLS_C := irt_syscalls.c
IRT_SYSCALLS_H := $(INS_ROOT)/../include/irt_syscalls.h

IRT_H := $(INS_ROOT)/../include/irt.h
IRT_DEV_H := $(INS_ROOT)/../include/irt_dev.h
IRT_SOCKET_H := $(SRC_ROOT)/../libc/arch-nacl/syscalls/irt_socket.h
IRT_POLL_H := $(SRC_ROOT)/../libc/arch-nacl/syscalls/irt_poll.h


irt : $(IRT_SYSCALLS_H)
$(IRT_SYSCALLS_C) $(IRT_SYSCALLS_H) : $(IRT_PY) $(IRT_H) $(IRT_DEV_H) $(IRT_SOCKET_H) $(IRT_POLL_H)
	python $(IRT_PY) -i $(IRT_SYSCALLS_H) -s $(IRT_SYSCALLS_C) $(IRT_H) $(IRT_DEV_H) $(IRT_SOCKET_H) $(IRT_POLL_H)

$(eval $(call BASIC_COMPILE_TARGET,$(basename $(IRT_SYSCALLS_C)).o,$(IRT_SYSCALLS_C),$(CC),-std=gnu99 -g $(CFLAGS) $(LIBC_CFLAGS) $(ARCH_FLAGS),,,$(IRT_SYSCALLS_H)))

clean:
	rm -f *.c *.d *.o
	rm -f $(IRT_SYSCALLS_H)